package cn.edu.nyist.integration.service.impl;

import cn.edu.nyist.integration.common.Const;
import cn.edu.nyist.integration.common.ServerResponse;
import cn.edu.nyist.integration.entity.Category;
import cn.edu.nyist.integration.entity.ProductCredit;
import cn.edu.nyist.integration.mapper.CategoryMapper;
import cn.edu.nyist.integration.mapper.ProductCreditMapper;
import cn.edu.nyist.integration.service.ProductService;
import cn.edu.nyist.integration.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductCreditMapper productCreditMapper;

    @Resource
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public PageInfo listProduct(int pageNum, int pageSize, Integer categoryId) {
        List<ProductCredit> creditList = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        creditList = productCreditMapper.selectAll(categoryId);
        PageInfo pageInfo = new PageInfo<>(creditList);
        pageInfo.setList(creditList);
        return pageInfo;
    }

    @Override
    public List<Category> listAllCategory() {
        //从缓存中获取分类，如果有直接返回
        List<Category> categoryList = (List<Category>) redisUtil.get("categoryList");
        if (!CollectionUtils.isEmpty(categoryList)){
            return categoryList;
        }
        //没有的话就查数据库，查完把结果装到缓存中
        List<Category> categorys =  categoryMapper.selectAll();

        redisUtil.set("categoryList",categorys);

        return categoryMapper.selectAll();
    }

    @Override
    public ServerResponse delProduct(Long productId) {
        if (productId == null){
            return ServerResponse.createByErrorMessage(Const.ILLEGAL_ARGUMENT);
        }
        //清空这个商品的缓存
        redisUtil.del(productId.toString());
        int rowCount = productCreditMapper.deleteById(productId);
        if (rowCount == 0){
            return ServerResponse.createByErrorMessage("删除失败");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse addProduct(ProductCredit productCredit) {
        productCredit.setCreateTime(new Date());
        productCredit.setUpdateTime(new Date());
        int row = productCreditMapper.insertBySelect(productCredit);
        if (row == 0){
            return ServerResponse.createByErrorMessage("添加失败请重试");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse updateProductScore(Integer score, String productName) {

        //修改商品积分时可以先删除缓存
        redisUtil.del(productName);

        int row = productCreditMapper.updateScoreByName(score,productName);
        if (row == 0){
            return ServerResponse.createByErrorMessage("修改失败");
        }
        return ServerResponse.createBySuccessMessage("修改成功");
    }

    /**
     * 根绝类别查名称
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse getProductNameByCategoryId(Integer categoryId) {
        if (categoryId == null){
            return ServerResponse.createByErrorMessage(Const.ILLEGAL_ARGUMENT);
        }
        List<String> resultList = productCreditMapper.selectProductNameByCategoryId(categoryId);
        return ServerResponse.createBySuccessData(resultList);
    }

    /**
     * 三级联动
     */
    
    @Override
    public ServerResponse<Map<String,Map>> getInsertData(){

        Map<String,Map> categoryMap=new HashMap<>();

        List<Category> categoryList = categoryMapper.selectAll();
        for (Category category : categoryList) {
            String categoryName = category.getCategoryName();
            List<ProductCredit> productCredits = productCreditMapper.selectByCategory(category.getCategoryId());
            Map<String,Integer> map = new HashMap<>();;
            for (ProductCredit productCredit : productCredits) {

                String productName = productCredit.getProductName();
                Integer productScore = productCredit.getProductScore();
                map.put(productName,productScore);

            }
            categoryMap.put(categoryName,map);

        }
       return  ServerResponse.createBySuccessData(categoryMap);
    }


    /**
     * 获取商品积分
     */

    @Override
    public ServerResponse getScoreByName(String productName){
        if (productName ==  null){
            return ServerResponse.createByErrorMessage(Const.ILLEGAL_ARGUMENT);
        }
        //从redis中获取商品积分，
        Integer redisScore = (Integer) redisUtil.get(productName);
        if (redisScore!=null){
            return ServerResponse.createBySuccessData(redisScore);
        }

        int score = productCreditMapper.selectScoreByName(productName);
        //添加缓存
        redisUtil.set(productName,score);
        return ServerResponse.createBySuccessData(score);
    }
}
